package com.kiseny.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.kiseny.po.Account;

public class AccountDAO extends BaseDAO<Account> {
    
    @SuppressWarnings("unchecked")
    public Account getAccountByName(String name) {
        List<Account> accountList = null;
        Account account = null;
        
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class);
        criteria.add(Restrictions.eq("deleteFlag", new Boolean(false)));
        
        criteria.add(Restrictions.eq("name", name));
        try {
            accountList = (List<Account>) getHibernateTemplate().findByCriteria(criteria);

        } catch (RuntimeException e) {
            getLogger().error("Fail to execute it.", e);
            throw e;
        }
        
        if(!accountList.isEmpty()) {
            account = accountList.get(0);
        }

        return account;
    }
     
    @SuppressWarnings("unchecked")
    public Account checkAccount(String name, String password) {
        List<Account> accountList = null;
        Account account = null;
        
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class);
        criteria.add(Restrictions.eq("deleteFlag", new Boolean(false)));
        
        criteria.add(Restrictions.eq("name", name));
        criteria.add(Restrictions.eq("password", password));
        
        try {
            accountList = (List<Account>) getHibernateTemplate().findByCriteria(criteria);

        } catch (RuntimeException e) {
            getLogger().error("Fail to execute it.", e);
            throw e;
        }
        
        if(!accountList.isEmpty()) {
            account = accountList.get(0);
        }
        
        return account;
    }
}
